<template>
  <section>
    <!-- 数据字典类型工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" clearable placeholder="关键字">
            <i slot="prefix" class="el-input__icon el-icon-search" style="position: static;"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getSysDicType">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="showDrawer">显示数据字典明细</el-button>
        </el-form-item>
      </el-form>
    </el-col>
<!--    @row-click="showDrawer"-->
    <!--数据字典类型列表-->
    <el-table :data="sysDicTypes"  @row-dblclick="getDetails"   highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="100"  label="序号">
      </el-table-column>
      <el-table-column prop="sn" label="数据字典类型编号" width="250" sortable>
      </el-table-column>
      <el-table-column prop="name" label="数据字典类型名称" width="250" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
<!--          <el-button type="success" size="small" @click.native.stop="getDetails(scope.$index, scope.row)">显示当前类型的明细</el-button>-->
          <el-button size="small" @click.native.stop="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click.native.stop="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--数据字典类型工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          background
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="totals"
          style="float:right;">
      </el-pagination>
    </el-col>
    <!-- 数据字典明细抽屉 -->
    <el-drawer
        title="数据字典明细"
        :visible.sync="drawer"
        :with-header="true"
        size="50%" >
      <!-- 数据字典明细工具条 -->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
        <el-form :inline="true" :model="detailFilters">
          <el-form-item>
            <el-input v-model="detailFilters.keyword" clearable placeholder="关键字">
              <i slot="prefix" class="el-input__icon el-icon-search" style="position: static;"></i>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="getSysDicDetail">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleDetailAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <!-- 数据字典明细表格 -->
      <el-table :data="detailTable" highlight-current-row @selection-change="selsDetailChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="100"  label="序号">
        </el-table-column>
        <el-table-column prop="name" label="数据字典名称" width="180" sortable>
        </el-table-column>
        <el-table-column prop="sysDicType.name" label="数据字典类型" width="180" sortable>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" @click.native.stop="handleDetailEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click.native.stop="handleDetailDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 数据字典明细&批量删除 -->
      <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchDetailRemove" :disabled="this.detailSels.length===0">批量删除</el-button>
        <!--  分页条      -->
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="handleDetailCurrentChange"
            :page-size="detailPageSize"
            :total="detailTotals"
            style="float:right;">
        </el-pagination>
      </el-col>
      <!-- 数据字典明细编辑界面 -->
      <el-dialog :title="typetitle" :visible.sync="sysDicDetailFormVisible" :append-to-body="isTrue" :close-on-click-modal="false">
        <el-form :model="sysDicDetailForm" label-width="200px" :rules="sysDicDetailFormRules" ref="sysDicTypeForm">
          <el-form-item label="数据名称" prop="name">
            <el-input v-model="sysDicDetailForm.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="数据类型" prop="typesId">
<!--            <el-input v-model="sysDicDetailForm.typesId" auto-complete="off"></el-input>-->
            <el-select  clearable  v-model="sysDicDetailForm.typesId"  filterable placeholder="请选择">
              <el-option
                  v-for="item in systemTypes"
                  :label="item.name"
                  :value="item.id">

              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="sysDicDetailFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="saveDetailSubmit" :loading="sysDicDetailLoading">提交</el-button>
        </div>
      </el-dialog>
    </el-drawer>

    <!-- 数据字典类型编辑界面 -->
    <el-dialog :title="typetitle" :visible.sync="sysDicTypeFormVisible" :close-on-click-modal="false">
      <el-form :model="sysDicTypeForm" label-width="200px" :rules="sysDicTypeFormRules" ref="sysDicTypeForm">
        <el-form-item label="数据字典类型编号" prop="sn">
          <el-input v-model="sysDicTypeForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据字典类型名称" prop="name">
          <el-input v-model="sysDicTypeForm.name" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="sysDicTypeFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="sysDicTypeLoading">提交</el-button>

      </div>
    </el-dialog>


    <!-- 双击类型弹出的详情表单   -->
    <el-dialog title="当前数据类型明细" :visible.sync="dataTable" width="500px">
      <el-table :data="details">
        <el-table-column type="index" width="100"  label="序号">
        </el-table-column>
        <el-table-column prop="name" label="数据字典类型编号" width="250" sortable>
        </el-table-column>
      </el-table>
    </el-dialog>


  </section>
</template>

<script>
export default {
  data() {
    return {
      // 弹出详情表单需要的数据
      details:[],
      dataTable: false,//双击详情表单默认关闭
      typedetails:{ // 克隆的数据
          id:null,
        name:'',
        typesId:null
      },


      systemTypes:[], // 数据字典类型


      typetitle:'', // 动态显示编辑和新增的标题
      isTrue: true,
      // 抽屉默认不显示
      drawer: false,
      // 数据字典类型高级查询
      filters: {
        name: '',
      },
      // 数据字典明细高级查询
      detailFilters: {
        typesId: null,
        keyword: '',
      },
      // 数据字典明细列表
      detailTable:[],
      // 数据字典类型列表
      sysDicTypes: [],
      // 类型分页相关
      totals: 0,
      currentPage: 1,
      pageSize:5,
      // 明细分页相关
      detailTotals: 0,
      detailCurrentPage: 1,
      detailPageSize:5,

      listLoading: false, //忙等框

      sels: [],//列表选中列

      detailSels: [],//列表明细选中列

      sysDicTypeFormVisible: false,//编辑新增界面是否显示
      sysDicTypeLoading: false,
      // 表单规则
      sysDicTypeFormRules: {
        name: [
          { required: true, message: '请输入数据字典类型名', trigger: 'blur' },
        ],
        sn: [
          { required: true, message: "请输入数据字典编码", trigger: "blur" },
        ]
      },
      //数据字典类型编辑界面数据
      sysDicTypeForm: {
        id: null,
        sn: "",
        name: "",

      },

      // 明细相关
      sysDicDetailFormVisible: false,//编辑新增界面是否显示
      sysDicDetailLoading: false,
      // 表单规则
      sysDicDetailFormRules: {
        name: [
          { required: true, message: '请输入数据字典明细名', trigger: 'blur' },
        ],
      },
      //数据字典明细编辑界面数据
      sysDicDetailForm: {
        id: null,
        name: "",
        typesId:null
      },
    }
  },
  methods: {


    // 获取所有的数据明细
    getDetails: function (row){
      let typesId = row.id;
      this.dataTable = true;
      this.typedetails = Object.assign({}, row);
      this.$http.get("/systemdictionarydetail/loadByTypeId/"+typesId).then(res=>{
      this.details = res.data;
      })
    },


    getSystemTypes(){
      this.$http.get("/systemdictionarytype").then(res=>{
        this.systemTypes = res.data;
      })
    },

    // 列抽屉表 初始化条件
    showDrawer(row){
      this.drawer = true;
      this.detailCurrentPage = 1;
      this.detailPageSize = 5;
      this.detailFilters.keyword = null;
      this.detailFilters.typesId = row.id;
      this.sysDicDetailForm.type ={"id" :row.id};
      this.getSysDicDetail();
    },
    // 类型换页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSysDicType();
    },
    // 明细换页
    handleDetailCurrentChange(val) {
      this.detailCurrentPage = val;
      this.getSysDicDetail();
    },
    // 获取明细列表
    getSysDicDetail() {
      let para = {
        currentPage: this.detailCurrentPage,
        pageSize: this.detailPageSize,
        keyword: this.detailFilters.keyword,
        typesId:this.detailFilters.typesId
      };
      this.$http.post("/systemdictionarydetail",para)
          .then(res=>{
            this.detailTable = res.data.data;
            this.detailTotals = res.data.totals;
            // 如果删掉的是当前页最后一个 就返回上一页
            if (this.detailCurrentPage !== 1 && this.detailCurrentPage > Math.ceil(this.detailTotals / this.detailPageSize)){
              this.detailCurrentPage -= 1;
              this.getSysDicDetail();
            }
          })
          .catch(()=>{
            this.$message({
              message: '服务器错误',
              type: 'error'
            });
          })
    },
    // 获取字典类型列表
    getSysDicType() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.name,
      };
      this.listLoading = true;
      this.$http.post("/systemdictionarytype",para)
          .then(res=>{
            this.listLoading = false;
            this.sysDicTypes = res.data.data;
            this.totals = res.data.totals;
            // 如果删掉的是当前页最后一个 就返回上一页
            if (this.currentPage !== 1 && this.currentPage > Math.ceil(this.totals / this.pageSize)){
              this.currentPage -= 1;
              this.getSysDicType();
            }
          })
          .catch(()=>{
            this.$message({
              message: '服务器错误',
              type: 'error'
            });
          })
    },
    //明细删除
    handleDetailDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.delete("/systemdictionarydetail/" + row.id)
            .then(()=>{
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.getSysDicDetail();
            })
            .catch(()=>{
              this.$message({
                message: '删除失败',
                type: 'warning'
              });
              this.getSysDicDetail();
            })
      }).catch(() => {});
    },
    //类型删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => { // 点击确认执行操作
        this.listLoading = true;  // 开启忙等框
        // 发送删除请求
        this.$http.delete("/systemdictionarytype/"+ row.id).then(res=>{
          // 关闭忙等框
          this.listLoading = false;
          if(res.data.success){
            // 删除成功
            this.$message({
              message: '删除成功',
              type: 'success'})
          }else {
            // 删除失败
            this.$message({
              message: '删除失败',
              type: 'error'})
          }

          // 重新刷新页面
          this.getSysDicType();
        }).catch(res=>{
          // 删除失败
          this.$message({
            message: res.data.message,
            type: 'error'})
        })
      }).catch(() => { // 点击取消后不执行操作

      });
    },
    //显示明细编辑界面
    handleDetailEdit: function (index, row) {
      this.getSystemTypes();
      this.typetitle = "编辑";
      this.sysDicDetailFormVisible = true;
      this.sysDicDetailForm = Object.assign({}, row);
    },

    //显示类型编辑界面
    handleEdit: function (index, row) {

      this.typetitle = "编辑";
      this.sysDicTypeFormVisible = true;
      this.sysDicTypeForm = Object.assign({}, row);
    },
    //显示明细新增界面
    handleDetailAdd: function () {
      this.getSystemTypes();
      this.typetitle = "新增";
      this.sysDicDetailFormVisible = true;
      this.sysDicDetailForm = {
          id: null,
          sn:'',
          name:'',
          typesId: null,

      }

    },
    //显示类型新增界面
    handleAdd: function () {
      this.typetitle = "新增";
      this.sysDicTypeFormVisible = true;
      this.sysDicTypeForm = {
        id: null,
        sn: "",
        name: "",
      };
    },
    // 类型新增修改
    saveSubmit: function () {
      this.$refs.sysDicTypeForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.sysDicTypeLoading = true;
            let para = Object.assign({}, this.sysDicTypeForm);
            this.$http.put("/systemdictionarytype",para).then(res=>{

              this.sysDicTypeLoading = false;
              if (res.data.success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: res.data.message,
                  type: 'error'
                });
              }
              this.sysDicTypeFormVisible = false;
              this.getSysDicType();

            }).catch(res=>{
              this.$message({
                message: '系统异常，骚后重试',
                type: 'error'
              });
            })


          });
        }
      });
    },
    // 明细新增修改
    saveDetailSubmit: function () {
      this.$refs.sysDicTypeForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.sysDicTypeLoading = true;
            let para = Object.assign({}, this.sysDicDetailForm);
            this.$http.put("/systemdictionarydetail", para).then((res) => {
              this.sysDicTypeLoading = false;
              if(res.data.success){
                this.$message({
                  message: res.data.message,
                  type: 'success'
                });
              } else {
                this.$message({
                  message: res.data.message,
                  type: 'warning'
                });
              }
              this.sysDicDetailFormVisible = false;
              this.getSysDicDetail();
            }).catch(()=>{
              this.$message({
                message: "服务器错误",
                type: 'error'
              });
            });
          });
        }
      });
    },
    // 明细多选
    selsDetailChange: function (sels) {
      this.detailSels = sels;
    },
    // 类型多选
    selsChange: function (sels) {
      this.sels = sels;
    },
    //类型批量删除
    batchRemove: function () {
      let ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/systemdictionarytype", ids).then((res) => {
          this.listLoading = false;
          if (res.data.success){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
          this.getSysDicType();
        }).catch(()=>{
          this.$message({
            message: "服务器错误",
            type: 'error'
          });
        });
      }).catch(() => {

      });
    },
    //明细批量删除
    batchDetailRemove: function () {
      let ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/systemdictionarydetail", ids).then((res) => {
          this.listLoading = false;
          if (res.data.success){
            this.$message({
              message: res.data.message,
              type: 'success'
            });
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            });
          }
          this.getSysDicDetail();
        }).catch(()=>{
          this.$message({
            message: "服务器错误",
            type: 'error'
          });
          this.getSysDicDetail();
        });
      }).catch(() => {

      });
  },
},
  mounted() {
    this.getSysDicType();
  }
}
</script>

<style scoped>

</style>